﻿// -----------------------------------------------------------------------
// <copyright file="Utilities.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMOMEventReceivers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;


    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Utilities
    {
        public static DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
        {
            DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
            return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
        }

        public static DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }

        /// <summary>
        /// This method is used to return an SPUser from a field in an SPListItem.
        /// </summary>
        /// <param name="item">Type: SPListItem. The ListItem in which we hope to find a user.</param>
        /// <param name="fieldName">The display name of the SPFieldUser field.</param>
        /// <returns>The SPUser object searched for. Otherwise, null.</returns>
        public static SPUser GetSPUser(SPListItem item, string fieldName)
        {
            SPUser user = null;
            SPField field = item.Fields[fieldName];
            SPFieldUserValue fieldValue = field.GetFieldValue(Convert.ToString(item[field.Title])) as SPFieldUserValue;
            if (fieldValue != null)
                user = fieldValue.User;

            return user;
        }
    }

}
